package com.vshop.controller.simple;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.vshop.entity.interfacerequest.ExceptionRequest;
import com.vshop.entity.interfacerequest.trademark.request.RequestTradeMark;
import com.vshop.entity.interfacerequest.trademark.request.RequestTradeMarkData;
import com.vshop.entity.interfacerequest.trademark.response.ResponseTradeMark;
import com.vshop.entity.interfacerequest.trademark.response.ResponseTradeMarkData;
import com.vshop.entity.interfacerequest.trademark.response.ResponseTradeMarkTradeData;
import com.vshop.service.SequenceService;
import com.vshop.service.SysOperateService;
import com.vshop.utils.Md5Encrypt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 获取
 *
 * @author lm
 * @date 2019/03/07
 */
@RestController
@Api(description = "企业商标")
@Slf4j
public class PartnerTradeMarkController {
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private SysOperateService operaterService;

    @CrossOrigin
    @PostMapping(value = "tradeMark", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "企业商标信息", response = ResponseTradeMark.class)
    public String tradeMark(@ApiParam @RequestBody RequestTradeMark request) {
        // 1，生成流水号
        String connSeqNo = sequenceService.getPackageTaskNo();
        // 2,生成当前时间戳
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curtime = df.format(new Date());
        //3,定义要回复的内容
        String jsonString;
        ExceptionRequest utils = new ExceptionRequest();

        String requestStr = JSON.toJSONString(request);

        if (StringUtils.isBlank(requestStr)) {
            jsonString = utils.dataWeiKong(connSeqNo, curtime);
            return jsonString;
        } else {
            RequestTradeMark requestPackage = JSONArray.parseObject(requestStr, RequestTradeMark.class);
            String requestSign = requestPackage.getSign();
            if (requestPackage.getSign() == null) {
                jsonString = utils.dataWeiKong(connSeqNo, curtime);
                return jsonString;
            } else {
                RequestTradeMarkData requestData = requestPackage.getData();
                if (requestPackage.getData() == null) {
                    jsonString = utils.dataWeiKong(connSeqNo, curtime);
                    return jsonString;
                } else {

                    String ConnSeqNo = requestData.getConnSeqNo();
                    String partnerCode = requestData.getPartnerCode();
                    String tradeTime = requestData.getTradeTime();
                    String tradeType = requestData.getTradeType();
                    String lang = requestData.getLang();
                    String version = requestData.getVersion();

                    //lang为空或者为null时，默认为cn
                    if (StringUtils.isBlank(ConnSeqNo) || StringUtils.isBlank(tradeTime) || StringUtils.isBlank(tradeType) || lang == null || version == null) {
                        jsonString = utils.dataWeiKong(connSeqNo, curtime);
                        return jsonString;
                    } else {
                        // 将请求体json转成String   // 将Str拿去计算签名
                        String str = JSON.toJSONString(requestData);
                        String sign = utils.calculateSignature(str);
                        if (!requestSign.equalsIgnoreCase(sign)) {
                            jsonString = utils.signatureFailure(ConnSeqNo, tradeTime, tradeType, "0001", "The signature is wrong, please confirm the signature!");
                            return jsonString;
                        } else {
                            //签名验证成功
                            ResponseTradeMarkTradeData tradeData = null;

                            if (StringUtils.isNotBlank(partnerCode)) {

                                //用partnerCode和continent去数据库查询这个大洲列表
                                String tradeMark = operaterService.queryTradeMarkByPartnerCode(partnerCode);

                                if (StringUtils.isNotBlank(tradeMark)) {

                                    tradeData = new ResponseTradeMarkTradeData();
                                    tradeData.setTradeMark(tradeMark);

                                    ResponseTradeMarkData data = new ResponseTradeMarkData();
                                    data.setStatus("1");
                                    data.setConnSeqNo(ConnSeqNo);
                                    data.setPartnerCode(partnerCode);
                                    data.setTradeType(tradeType);
                                    data.setTradeTime(tradeTime);
                                    data.setTradeRstCode("0000");
                                    data.setTradeRstMessage("成功");
                                    data.setTradeData(tradeData);

                                    ResponseTradeMark re = new ResponseTradeMark();
                                    re.setData(data);
                                    re.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));
                                    jsonString = JSON.toJSONString(re);
                                    return jsonString;


                                } else {

                                    ResponseTradeMarkData data = new ResponseTradeMarkData();
                                    data.setStatus("0");
                                    data.setConnSeqNo(ConnSeqNo);
                                    data.setPartnerCode(partnerCode);
                                    data.setTradeType(tradeType);
                                    data.setTradeTime(tradeTime);
                                    data.setTradeRstCode("0000");
                                    data.setTradeRstMessage("成功!");
                                    data.setTradeData(tradeData);

                                    ResponseTradeMark re = new ResponseTradeMark();
                                    re.setData(data);
                                    re.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

                                    jsonString = JSON.toJSONString(re);
                                    return jsonString;


                                }
                            } else {

                                ResponseTradeMarkData data = new ResponseTradeMarkData();
                                data.setStatus("0");
                                data.setConnSeqNo(ConnSeqNo);
                                data.setPartnerCode(partnerCode);
                                data.setTradeType(tradeType);
                                data.setTradeTime(tradeTime);
                                data.setTradeRstCode("0000");
                                data.setTradeRstMessage("成功");
                                data.setTradeData(tradeData);

                                ResponseTradeMark re = new ResponseTradeMark();
                                re.setData(data);
                                re.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

                                jsonString = JSON.toJSONString(re);
                                return jsonString;


                            }

                        }


                    }
                }
            }
        }

    }

}
